from lxml import etree

class BtTianTang(object):

    def __init__(self, parser):
        self.parser = parser
        self.domain = 'https://www.bttiantang.la'

    def parse(self, layer, content):
        if layer == 'first':
            self.first(content)
        elif layer == 'second':
            self.second(content)
            # etree.Html(content)
            # print("parser")

    def first(self, content):
        html = etree.HTML(content)
        # //ul[@id='post_list']/li/div[@class='article']/h2/a/@ href
        urls = html.xpath("//ul[@id='post_list']/li/div[@class='article']/h2/a/@ href")
        for url in urls:
            if 'http' not in url:
                url = self.domain + url
            self.parser.add_url(('bttiantang', 'second', url))

    def is_http(self, url):
        return url.split('/')[0]

    def second(self, content):
        html = etree.HTML(content)
        title = html.xpath("//div[@class='article_container row box']/h1")
        img = html.xpath("//div[@class='article_container row box']/div[@class='context']/div[@id='post_content']/p[@class='tpic-cont-s']/img/@src")

